package com.personal.system.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.personal.system.config.SecurityUtil;
import com.personal.system.entity.content.ImMessage;
import com.personal.system.mapper.content.ImMessageMapper;
import com.xxl.job.core.util.IpUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SystemNoticeHandle {

    /**
     * 发送系统通知
     * @param toUserId
     * @param noticeType
     * @param noticeCode
     */
    public static void sendNotice(String toUserId,Integer noticeType,Integer noticeCode,Integer articleId,Integer commentMark,String content) {
        ImMessageMapper imMessageMapper = SpringUtil.getBean(ImMessageMapper.class);
        SecurityUtil securityUtil = SpringUtil.getBean(SecurityUtil.class);
        try {
            String ip = IpUtil.getIp();

            ImMessage message = ImMessage.builder().fromUserId(securityUtil.getUserId().toString()).toUserId(toUserId).content(content).commentMark(commentMark)
                    .noticeType(noticeType).code(noticeCode).ip(ip).ipSource("").articleId(articleId).build();
            imMessageMapper.insert(message);
        } catch (Exception e) {
            // 添加失败的话不抛异常，还是要执行成功
            log.error("生成消息通知失败，错误原因：{}",e);
        }
    }

}
